package com.msmall.community.bbs.action.html;

import com.msmall.base.cache.CacheFactory;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.bbs.model.Board;
import com.msmall.community.bbs.model.Thread;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.bbs.service.BoardManager;
import com.msmall.community.bbs.service.TopicManager;
import com.msmall.community.bbs.util.BbsUtil;
import com.msmall.community.common.CascadeCollection;

public class TopicAction extends BBSBaseAction{


	private static final long serialVersionUID = -2630077905824463628L;
	
	/**
	 * 回复发表人 follower
	 */
	private Long fer;
	
	private TopicManager topicManager;
	
	private BoardManager boardManager;
	
	/**
	 * 查看帖子, 增加点击数
	 * @return
	 */
	public String execute(){
		CascadeCollection<Board> cs = (CascadeCollection<Board>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_BOARD_CASCADECOLLECTION);
		try{
			topic = (Topic)topicManager.get(Topic.class, tid);
		}catch(Exception e){
			return "boardIndex";
		}
		if(message == null){ // 当发表回复时，出错挑战到此，需要从SESSION中取得出错信息
			message = (String) getFromSession(SESSION_MESSAGE_KEY);
			if(message!= null && getText("common.authentication.anonymous").equalsIgnoreCase(message)){
				if(getIsLogin()){
					message = null;
					removeFromSession(SESSION_MESSAGE_KEY);
				}
			}
		}
		if(thread == null){ // 当发表回复时，出错挑战到此，需要从SESSION中取得出错信息
			thread = (Thread) getFromSession(SESSION_THREAD_KEY);
		}
		bid = topic.getBoardid();
		if(topic.getEnabled().compareTo((short)0) == 0){
			return "boardIndex";
		}
		topicManager.addClickAmount(topic);
		board = cs.getAllDatas().get(bid).getCurrent();
		
		page.setPageSize(30);
		topicManager.getThreadPaginationByTopic(page, getUserid(), tid, null);
		page.setUrl(ConstantUrl.HTML_TOPIC_VIEW + "?bid="+ bid+"&tid="+tid);
		return SUCCESS;
	}

	/**
	 * 进入创建帖子页面
	 * @return
	 */
	public String create(){
		if(topic == null){
			topic = (Topic)getFromSession(SESSION_TOPIC_KEY);
		}
		if(message == null){
			message = (String) getFromSession(SESSION_MESSAGE_KEY);
			if(message!= null && getText("common.authentication.anonymous").equalsIgnoreCase(message)){
				if(getIsLogin()){
					message = null;
					removeFromSession(SESSION_MESSAGE_KEY);
				}
			}
		}
		if(bid == null || bid.longValue() == 0l){
			bid = BbsUtil.getBBSDefaultBoard();
		}
		board = boardManager.getBoard(bid);
		return SUCCESS;
	}
	/**
	 * 保存帖子，用于创建和修改
	 * @return
	 */
	public String save(){
		if(!getIsLogin()){
			message = getText("common.authentication.anonymous");
			setToSession(SESSION_TOPIC_KEY, topic);
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		/*
		if(topic == null){
			setToSession(SESSION_TOPIC_KEY, topic);
			message = getText("html.bbs.topic.create.title.length.short");
			return INPUT;
		}
		*/
		int validtitle = topicManager.validateTopicTitle(topic.getTitle());
		if(validtitle == 2){
			setToSession(SESSION_TOPIC_KEY, topic);
			message = getText("html.bbs.topic.create.title.length.short");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		if(validtitle == 3){
			setToSession(SESSION_TOPIC_KEY, topic);
			message = getText("html.bbs.topic.create.title.length.long");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		if(validtitle == 4){
			setToSession(SESSION_TOPIC_KEY, topic);
			message = getText("html.bbs.topic.create.title.illegal");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		int validcontent = topicManager.validateTopicContent(topic.getContent());
		if(validcontent == 2){
			setToSession(SESSION_TOPIC_KEY, topic);
			message = getText("html.bbs.topic.create.content.length.short");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		if(validcontent == 3){
			setToSession(SESSION_TOPIC_KEY, topic);
			message = getText("html.bbs.topic.create.content.length.long");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		if(validcontent == 4){
			setToSession(SESSION_TOPIC_KEY, topic);
			message = getText("html.bbs.topic.create.content.illegal");
			setToSession(SESSION_MESSAGE_KEY, message);
			return INPUT;
		}
		topic.setNickname(getNickname());
		topic = topicManager.saveTopic(topic, bid, getUserid(), userAgentType );
		removeFromSession(SESSION_TOPIC_KEY); //清空
		removeFromSession(SESSION_MESSAGE_KEY); //清空
		return SUCCESS;
	}

	/**
	 * 只看楼主，显示主题帖和回复
	 * @return
	 */
	public String viewTopicOwner(){
		CascadeCollection<Board> cs = (CascadeCollection<Board>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_BOARD_CASCADECOLLECTION);
		
		topic = (Topic)topicManager.get(Topic.class, tid);
		bid= topic.getBoardid();
		board = cs.getAllDatas().get(bid).getCurrent();
		

		topicManager.getThreadPaginationByTopic(page, getUserid(), tid, topic.getUserid());
		page.setUrl(ConstantUrl.HTML_TOPIC_VIEW_OWNER + "?tid="+tid);
		return SUCCESS;
	}
	
	/**
	 * 只看他，显示主题帖和回复
	 * @return
	 */
	public String viewTopicWho(){
		CascadeCollection<Board> cs = (CascadeCollection<Board>) CacheFactory.getInstance().getCache(Constants.CACHE_NAME_COMMON).get(Constants.CACHE_KEY_BBS_BOARD_CASCADECOLLECTION);
		
		topic = (Topic)topicManager.get(Topic.class, tid);
		bid= topic.getBoardid();
		board = cs.getAllDatas().get(bid).getCurrent();

		topicManager.getThreadPaginationByTopic(page, getUserid(), tid, fer);
		page.initPageString(getRequest());
		
		return "view";
	}
	
	public Long getFer() {
		return fer;
	}
	public void setFer(Long fer) {
		this.fer = fer;
	}

	public void setTopicManager(TopicManager topicManager) {
		this.topicManager = topicManager;
	}
	
	public void setBoardManager(BoardManager boardManager) {
		this.boardManager = boardManager;
	}
}
